/*
 * Chastoslov
 * Copyright (C) 2009 Alexandr Stroghov
 * 
 * stroghov@gmail.com
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License, version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if 
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307 USA
*/
package world.ah.wf.guiclient;
import java.awt.Dimension;
import java.awt.Frame;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.event.ComponentEvent;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class DialogDeleteText extends JDialog {
  private Connection conn;
  public void setConn(Connection conn) {this.conn = conn;};
  //
  private JLabel labText = new JLabel();
  private JComboBox combText = new JComboBox();
  private JButton bDelete = new JButton();
  private JButton bClose = new JButton();
  private JPanel pCompensator = new JPanel();
  private GridBagLayout gridBagLayout1 = new GridBagLayout();
  
  public DialogDeleteText() {this(null, "", false);}

  public DialogDeleteText(Frame parent, String title, boolean modal) {
    super(parent, title, modal);
    try {jbInit();} catch(Exception e) {e.printStackTrace();}
    //
    General.centerFrame(this);
    // Язык (не предполагается переустановка языка во время работы с формой)
    setLang();
    //
  }
  //
  private void jbInit() throws Exception {
    this.setSize(new Dimension(400, 188));
    this.getContentPane().setLayout(gridBagLayout1);
    this.setTitle("Delete text");
    this.setResizable(false);
    this.addComponentListener(new java.awt.event.ComponentAdapter()
      {
        public void componentShown(ComponentEvent e)
        {
          this_componentShown(e);
        }
      });
    labText.setText("Choose text");
    labText.setLabelFor(combText);
    bDelete.setText("Delete...");    
    bDelete.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          bDelete_actionPerformed(e);
        }
      });
    bClose.setText("Close");
    bClose.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          bCancel_actionPerformed(e);
        }
      });
    this.getContentPane().add(combText, new GridBagConstraints(0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(3, 10, 3, 10), 0, 0));
    this.getContentPane().add(labText, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(10, 13, 0, 13), 0, 0));
    this.getContentPane().add(pCompensator, new GridBagConstraints(0, 2, 2, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(bDelete, new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(10, 10, 10, 5), 0, 0));
    this.getContentPane().add(bClose, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(10, 5, 10, 10), 0, 0));
  }

  private void this_componentShown(ComponentEvent e) {
    //System.out.println("=================================================");
    //System.out.println("CompensatorHeight=" + pCompensator.getHeight());
    //System.out.println("DialogHeight0=" + this.getHeight());
    this.setSize(this.getWidth(), this.getHeight() - pCompensator.getHeight() + 7);
    //System.out.println("DialogHeight1=" + this.getHeight());
    // Список текстов 
    try {General.fillTextList(combText, conn);} 
    catch (Exception ex) {ErrorShower.show(this, "loading list of texts", "", ex);}  
  }

  private void bCancel_actionPerformed(ActionEvent e) {
    this.setVisible(false);
  }
  //   
  private void bDelete_actionPerformed(ActionEvent e) {
    if(combText.getSelectedIndex() < 0) {
        JOptionPane.showMessageDialog(this, General.getLangItem("TextNotChosen", 
          "Text not chosen"), General.getLangItem("ErrorExcl", "Error!"), 
          JOptionPane.ERROR_MESSAGE); 
      combText.requestFocusInWindow();
      return;
    }
    int confirmResult = JOptionPane.showConfirmDialog(this, General.getLangItem("DeleteAppearance", 
      "Words appearance in the text will be cleared. Words \nin themselves won't be deleted. " + 
      "Also information \nabout checked words by users will remain. \nContinue?"), 
      General.getLangItem("Question", "Question"), JOptionPane.YES_NO_OPTION); 
    if(confirmResult == JOptionPane.YES_OPTION) {
      try {      
        PreparedStatement st = conn.prepareStatement("delete from APPEARANCE where TEXT_ID = ?"); 
        int textId = ((KeyedItem)(combText.getSelectedItem())).getKey();
        st.setInt(1, textId);
        st.executeUpdate();
        JOptionPane.showMessageDialog(this, General.getLangItem("NowNoWord", 
          "Now this text doesn't have no words"), General.getLangItem("DoneExcl", "Done!"), 
          JOptionPane.INFORMATION_MESSAGE); 
      } catch (Exception ex) {
        ErrorShower.show(this, "deleting text material", "", ex);
        return;
      }
    }
  }
  /** Устанавливает язык интерфейса */
  public void setLang() {
    String s;
    s = General.getLangItem("DeleteTextMaterial"); if(s.length() > 0) this.setTitle(s);
    s = General.getLangItem("ChooseText"); if(s.length() > 0) labText.setText(s);
    s = General.getLangItem("DeleteText3dots"); if(s.length() > 0) bDelete.setText(s);    
    s = General.getLangItem("Close"); if(s.length() > 0) bClose.setText(s);
  }


}
